home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / progbar.exe / EXAMPLE.CPP < prev    next >
C/C++ Source or Header  |  1992-10-12  |  6KB  |  233 lines

  1. // This is an example of a progress bar.
  2. //
  3. // Modified THERMO.ZIP in the C++ lib to work with TURBO VISION
  4. // and fixed a small bug.
  5. //
  6. // Author: Jay Perez. Modified by Barnaby Falls
  7.  
  8. #define Uses_TBackground
  9. #define Uses_TListBox
  10. #define Uses_TMenu
  11. #define Uses_TMenuBar
  12. #define Uses_TMenuItem
  13. #define Uses_TScrollBar
  14. #define Uses_TStaticText
  15. #define Uses_TStatusDef
  16. #define Uses_TStatusItem
  17. #define Uses_TStatusLine
  18. #define Uses_TStringCollection
  19. #define Uses_MsgBox
  20. #define Uses_TEventQueue
  21. #define Uses_TApplication
  22. #define Uses_TRect
  23. #define Uses_TDeskTop
  24. #define Uses_TView
  25. #define Uses_TWindow
  26. #define Uses_TDialog
  27. #define Uses_TButton
  28. #define Uses_StaticText
  29. #define Uses_TSItem
  30. #define Uses_TLabel
  31. #define Uses_TInputLine
  32. #define Uses_TEvent
  33. #define Uses_TKeys
  34. #define Uses_TDrawBuffer
  35. #define Uses_TStreamableClass
  36. #define Uses_TStreamable
  37.  
  38. #include <tv.h>
  39. __link( RView )
  40. __link( RDialog )
  41. __link( RButton )
  42. #include <mem.h>                       // memset
  43.  
  44. #include <dos.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #include <ctype.h>
  49. #include <strstrea.h>
  50. #include <iomanip.h>
  51. #include <conio.h>
  52.  
  53. #include "tprogbar.h"
  54.  
  55. const cmAboutCmd   = 100;  // User selected menu item 'About'
  56. const cmStatusCmd  = 101;  // User selected menu item 'Progress Bar'
  57.  
  58. //========================================================================
  59.  
  60. class TMyApplication : public TApplication
  61. {
  62. public:
  63.    TMyApplication();
  64.    static TMenuBar *initMenuBar(TRect);
  65.    void handleEvent(TEvent &);
  66. private:
  67.    void aboutDlg();
  68.    void statusDlg();
  69.    Boolean isCancel(TDialog *pd);
  70. };
  71.  
  72. //========================================================================
  73.  
  74. TMyApplication::TMyApplication() :
  75. TProgInit(&TApplication::initStatusLine,&TMyApplication::initMenuBar,
  76.      &TApplication::initDeskTop)
  77. {
  78. }
  79.  
  80. //========================================================================
  81.  
  82. TMenuBar *TMyApplication::initMenuBar(TRect bounds)
  83. {
  84.      bounds.b.y = bounds.a.y + 1;
  85.      return(new TMenuBar(bounds,
  86.                 new TMenu(
  87.                      *new TMenuItem("~A~bout",cmAboutCmd,kbAltA,hcNoContext,0,
  88.                           new TMenuItem("~P~rogress Bar",cmStatusCmd,kbAltL,hcNoContext,0)))));
  89. }
  90.  
  91. //========================================================================
  92.  
  93. void TMyApplication::handleEvent(TEvent &event)
  94. {
  95.      TApplication::handleEvent(event);
  96.  
  97.      if (event.what == evCommand)
  98.      {
  99.           switch (event.message.command)
  100.           {
  101.           case cmAboutCmd:
  102.           {
  103.                 aboutDlg();
  104.                 clearEvent(event);
  105.                 break;
  106.           }
  107.           case cmStatusCmd:
  108.           {
  109.                 statusDlg();
  110.                 clearEvent(event);
  111.                 break;
  112.           }
  113.           }
  114.      }
  115. }
  116.  
  117. //========================================================================
  118.  
  119. void TMyApplication::aboutDlg()
  120. {
  121.      TDialog *pd = new TDialog(TRect(0,0,35,12),"About");
  122.      if (pd)
  123.      {
  124.     pd->options |= ofCentered;
  125.     pd->insert(new TStaticText(TRect(1,2,34,7),
  126.         "\003Turbo Vision Example\n\003\n"
  127.                      "\003Using a Progress Bar\n\003\n"));
  128.           pd->insert(new TButton(TRect(3,9,32,11),"~O~k",cmOK,bfDefault));
  129.  
  130.     if (validView(pd) != 0)
  131.     {
  132.                 deskTop->execView(pd);
  133.  
  134.         destroy(pd);
  135.     }
  136.      }
  137. }
  138.  
  139. //========================================================================
  140.  
  141. Boolean TMyApplication::isCancel(TDialog *pd)
  142. {
  143.    TEvent event;
  144.    pd->getEvent(event);
  145.    pd->handleEvent(event);
  146.    if(event.what==evCommand && event.message.command==cmCancel)
  147.       return (messageBox("Are you sure you want to Cancel",mfConfirmation|mfYesButton|mfNoButton)==cmYes ? True : False);
  148.    else
  149.       return False;
  150. }
  151.  
  152. void TMyApplication::statusDlg()
  153. {
  154.    TDialog *pd = new TDialog(TRect(0,0,60,15),"Example Progress Bar");
  155.    pd->flags &= ~wfClose;
  156.    pd->options |= ofCentered;
  157.    TProgressBar *pbar = new TProgressBar(TRect(2,2,pd->size.x-2,3),300);
  158.    pd->insert(pbar);
  159.    pd->insert(new TButton(TRect(10,pd->size.y-3,pd->size.x-10,pd->size.y-1),"~C~ancel",cmCancel,bfDefault));
  160.    TProgram::deskTop->insert(pd);     // Modeless !!!!
  161.  
  162.    int i=0;
  163.    Boolean keepOnGoing=True;
  164.  
  165.    TRect r(5,5,pd->size.x-5,pd->size.y-5);
  166.    TStaticText *theMessage;
  167.  
  168.    // The first 3rd.
  169.    theMessage = new TStaticText( r,
  170.       "This is a MODELESS dialog box. You can drag this box around the desktop." );
  171.    pd->insert(theMessage);
  172.    for(;i<=100;i++) {
  173.       pbar->update(i);
  174.       idle();
  175.       if(isCancel(pd)) {
  176.      keepOnGoing=False;
  177.      break;
  178.      }
  179.       delay(50);
  180.       }
  181.    destroy(theMessage);
  182.  
  183.    if(keepOnGoing) {
  184.       // The second 3rd
  185.       theMessage = new TStaticText( r,
  186.      "Notice that only the attribute is changed to show progress" );
  187.       pd->insert(theMessage);
  188.       for(;i<=200;i++) {
  189.      pbar->update(i);
  190.      idle();
  191.      if(isCancel(pd)) {
  192.         keepOnGoing=False;
  193.         break;
  194.         }
  195.      delay(50);
  196.      }
  197.       destroy(theMessage);
  198.       }
  199.  
  200.    if(keepOnGoing) {
  201.       // The last 3rd
  202.       theMessage = new TStaticText( r,
  203.      "Syntax: TProgressBar(TRect &r, double total, char bar);" );
  204.       pd->insert(theMessage);
  205.       for(;i<=300;i++) {
  206.      pbar->update(i);
  207.      idle();
  208.      if(isCancel(pd)) {
  209.         keepOnGoing=False;
  210.         break;
  211.         }
  212.      delay(50);
  213.      }
  214.       destroy(theMessage); // not necessary since we destroy
  215.                // the TDialog also
  216.       }
  217.    destroy(pd);
  218.  
  219.  
  220. }
  221.  
  222. //========================================================================
  223.  
  224. int main(void)
  225. {
  226.      TMyApplication myApplication;
  227.  
  228.      myApplication.run();
  229.  
  230.      return 0;
  231. }
  232.  
  233.